#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: process.sh
# Author: GID5564
# Description:
# Created Time: 19/11/23-21:55:01
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash


# 避免重复导入
#[[ -n $__XLIB_IMPORTED__PROCESS ]] && return 0
#__XLIB_IMPORTED__PROCESS=1

function __process_init__() {
    wait
}



########################################
# 功能: 检查一个进程是否存在
# 参数: $1 --- 要检查的进程名称（可以加入路径，增强唯一性）
# 返回: 如果存在返回1, 否则返回0.
########################################
function check_process(){
    [[ -z "$1" ]] && {
        error "Usage: check_process <process name>"
        return $INVALID_ARGUMENTS
    }
     
    local process_num=`ps -ef | grep "$1" | grep -v "grep" | grep -v "${XLIB_PROCESS##/*}" | wc -l`
    [[ ${process_num} -lt 1 ]] && echo 0 || echo 1
}
 
########################################
# 功能: 获取进程PID
# 参数: $1 --- 要检查的进程名称（可以加入路径，增强唯一性）
# 返回: PID
########################################
function get_pid(){
    [[ -z "$1" ]] && {
        error "Usage: get_pid <process name>"
        return $INVALID_ARGUMENTS
    }
    echo $(pgrep -x ${1})
}

########################################
# 功能: 获取进程PPID
# 参数: $1 --- 要检查的进程PID（可以加入路径，增强唯一性）
# 返回: PPID
########################################
function get_ppid() {
    [[ -z "$1" ]] && {
        error "Usage: get_ppid <PID>"
        return $INVALID_ARGUMENTS
    }
    local isn=$(IsNumber $1)
    [[ $isn -eq 0 ]] && {
        error "Usage: get_ppid <PID>,<PID> Is an integer!"
        return $INVALID_ARGUMENTS
    }

    local ppid=$(ps -ef|awk '$2=='${1}'{print $3}')
    printf "%s" "$ppid"
    unset ppid
    unset isn
}


########################################
# 功能: 获取进程UID
# 参数: $1 --- 要检查的进程PID（可以加入路径，增强唯一性）
# 返回: UID
########################################
function get_uid() {
    [[ -z "$1" ]] && {
        error "Usage: get_uid <PID>"
        return $INVALID_ARGUMENTS
    }
    local isn=$(IsNumber $1)
    [[ $isn -eq 0 ]] && {
        error "Usage: get_uid <PID>,<PID> Is an integer!"
        return $INVALID_ARGUMENTS
    }

    local uid=$(ps -ef|awk '$2=='${1}'{print $1}')
    printf "%s" "$uid"
    unset uid
    unset isn
}

########################################
# 功能: 获取用户名
# 参数: $1 --- UID
# 返回: username
########################################
function get_user() {
    [[ -z "$1" ]] && {
        error "Usage: get_user <UID>"
        return $INVALID_ARGUMENTS
    }
    local isn=$(IsNumber $1)
    [[ $isn -eq 0 ]] && {
        error "Usage: get_user <UID>,<UID> Is an integer!"
        return $INVALID_ARGUMENTS
    }

    local username=$(id ${1} | awk -F'(' '{print $2}' | cut -d')' -f1)
    
    printf "%s" "$username"
    unset username
    unset isn
}

########################################
# 功能: kill进程
# 参数: $1 --- PID
# 返回: 1:成功 0:失败
########################################
function kill_process() {
    [[ -z "$1" ]] && {
        error "Usage: kill_process <PID>"
        return $INVALID_ARGUMENTS
    }
    local isn=$(IsNumber $1)
    [[ $isn -eq 0 ]] && {
        error "Usage: kill_process <PID>,<PID> Is an integer!"
        return $INVALID_ARGUMENTS
    }
    kill -9 ${1}
    [[ $? -eq 0 ]] && echo 1 || echo 0
    unset isn
}



__process_init__




















































